package com.koocloud.electroplatemanage.service.impl;

import cn.hutool.core.lang.Validator;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koocloud.electroplatemanage.common.pojo.ResponseTemplate;
import com.koocloud.electroplatemanage.mapper.QuotationMapper;
import com.koocloud.electroplatemanage.pojo.Quotation;
import com.koocloud.electroplatemanage.service.QuotationService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @program:
 * @description: 镀槽服务层实现类
 * @author: wjx
 * @create: 2020/11/12 17:56
 */
@Service
@Transactional(rollbackFor=Exception.class)
public class QuotationServiceImpl implements QuotationService {

    @Resource
    QuotationMapper quotationMapper;
    Lock lock = new ReentrantLock();

    @Override
    public ResponseTemplate selectByPk(int pk) {
        try {
            //新建工种实体
            Quotation quotation = null;
            quotation = quotationMapper.selectByPk(pk);
            //判断查表操作是否成功
            if (null != quotation) {
                //查表成功返回数据
                return ResponseTemplate.builder().code(0).message("查询成功").count(1l).data(quotation).build();
            }
            else {
                //查表失败返回提示
                return ResponseTemplate.builder().code(1).message("没有此数据").count(0l).build();
            }
        } catch (Exception e) {
            //遇到异常，返回异常原因
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResponseTemplate.builder().code(1).message(e.getMessage()).count(0l).build();
        }
    }

    @Override
    public ResponseTemplate selectByName(String name) {
        try {
            //新建工种实体
            Quotation quotation = null;
            quotation = quotationMapper.selectByName(name);
            //判断查表操作是否成功
            if (null != quotation) {
                //查表成功返回数据
                return ResponseTemplate.builder().code(0).message("查询成功").count(1l).data(quotation).build();
            }
            else {
                //查表失败返回提示
                return ResponseTemplate.builder().code(1).message("没有此数据").count(0l).build();
            }
        } catch (Exception e) {
            //遇到异常，返回异常原因
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResponseTemplate.builder().code(1).message(e.getMessage()).count(0l).build();
        }
    }

    @Override
    public ResponseTemplate selectAllData(String page, String limit, Quotation quotation) {
        //校验分页参数是否为纯数字 并且page和limit都不等于0
        if(Validator.isNumber(page)&&Validator.isNumber(limit)&&!("0".equals(page)&&"0".equals(limit))){
            try{
                //开始分页
                PageHelper.startPage(Integer.valueOf(page),Integer.valueOf(limit));
                List<Quotation> quotationList = new ArrayList<>();
                quotationList = quotationMapper.selectAllData(quotation);
                PageInfo<Quotation> info = new PageInfo<>(quotationList);
                if (info.getTotal()>0){
                    return ResponseTemplate.builder().code(0).message("查询成功").count(info.getTotal()).data(info.getList()).build();
                }else {
                    //没有查询到数据
                    return ResponseTemplate.builder().code(1).message("没有查到对应数据").count(0l).build();
                }
            } catch (Exception e) {
                //遇到异常，返回异常原因
                e.printStackTrace();
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return ResponseTemplate.builder().code(1).message(e.getMessage()).count(0l).build();
            }
        }else {
            return ResponseTemplate.builder().code(1).message("分页参数错误").count(0l).build();
        }
    }

    @Override
    public ResponseTemplate selectAllByName(String page, String limit,String name) {
        //校验分页参数是否为纯数字 并且page和limit都不等于0
        if(Validator.isNumber(page)&&Validator.isNumber(limit)&&!("0".equals(page)&&"0".equals(limit))){
            try{
                //开始分页
                PageHelper.startPage(Integer.valueOf(page),Integer.valueOf(limit));
                List<Quotation> quotationList = new ArrayList<>();
                quotationList = quotationMapper.selectAllByName(name);
                PageInfo<Quotation> info = new PageInfo<>(quotationList);
                if (info.getTotal()>0){
                    return ResponseTemplate.builder().code(0).message("查询成功").count(info.getTotal()).data(info.getList()).build();
                }else {
                    //没有查询到数据
                    return ResponseTemplate.builder().code(1).message("没有查到对应数据").count(0l).build();
                }
            } catch (Exception e) {
                //遇到异常，返回异常原因
                e.printStackTrace();
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return ResponseTemplate.builder().code(1).message(e.getMessage()).count(0l).build();
            }
        }else {
            return ResponseTemplate.builder().code(1).message("分页参数错误").count(0l).build();
        }
    }

    /**
     * @Description://插入一条记录
     * @param quotation
     * @date: 2020/11/15
     * @Author: wjx
     * @return
     */
    //添加镀种只能从镀种表里添加 用下拉框
    @Override
    public ResponseTemplate insert(Quotation quotation) {

        try{
            lock.lock();
            Quotation quotation1 = quotationMapper.selectByName(quotation.getName());
            if(!StringUtils.isEmpty(quotation1)){
                //存在
                return ResponseTemplate.builder().code(1).message("该镀种已存在").count(0l).build();
            }else {
                int rows = quotationMapper.insert(quotation);
                if (rows > 0){
                    return ResponseTemplate.builder().code(0).message("插入数据成功").count(1l).build();
                } else {//没有异常并且插入数据失败，原则上不出现
                    return ResponseTemplate.builder().code(1).message("插入数据失败").count(0l).build();
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResponseTemplate.builder().code(1).message(e.getMessage()).count(0l).build();
        }finally {
            lock.unlock();
        }
    }

    /**
     *@Description:批量插入记录
     *@Param: [listQuotation]
     *@return: com.koocloud.electroplatemanage.common.ResponseTemplate
     *@Author: wjx
     *@date: 2020/11/16
     */
    @Override
    public ResponseTemplate batchInsert(List<Quotation> listQuotation) {
        if(!listQuotation.isEmpty()){
            try{
                lock.lock();
                //没有异常并且插入数据大于0
                int rows = quotationMapper.batchInsert(listQuotation);
                if(rows>0){
                    return ResponseTemplate.builder().code(0).message("批量插入数据成功").count((long)rows).build();
                }else{
                    //没有异常并且插入数据失败，原则上不出现
                    return ResponseTemplate.builder().code(1).message("批量插入数据失败").count(0l).build();
                }
            }catch (Exception e) {
                //出现异常返回异常原因
                e.printStackTrace();
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return ResponseTemplate.builder().code(1).message(e.getMessage()).count(0l).build();
            }finally {
                lock.unlock();
            }
        }else {
            return ResponseTemplate.builder().code(1).message("传入参数为空").count(0l).build();
        }
    }

    @Override
    public ResponseTemplate delete(String pk, String deleteFlag) {
        try {
            lock.lock();
            int rows = quotationMapper.delete(pk,deleteFlag);
            //判断删除操作是否成功
            if (rows > 0) {
                //删除成功返回数据
                return ResponseTemplate.builder().code(0).message("删除成功").count(1l).build();
            }
            else {
                //删除失败返回提示
                return ResponseTemplate.builder().code(1).message("删除失败").count(0l).build();
            }
        } catch (Exception e) {
            //遇到异常，返回异常原因
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResponseTemplate.builder().code(1).message(e.getMessage()).count(0l).build();
        }finally {
            lock.unlock();
        }
    }

    @Override
    public ResponseTemplate batchDeleteByPks(List<String> listPks) {
        if(!listPks.isEmpty()) {
            try {
                //存在合法pk值，进行批量删除操作
                int rows = quotationMapper.batchDeleteByPks(listPks);
                //判断批量删除操作是否成功
                if (rows > 0) {
                    //批量删除成功返回数据
                    return ResponseTemplate.builder().code(0).message("批量删除成功").count((long)rows).build();
                } else {
                    //批量删除失败返回提示
                    return ResponseTemplate.builder().code(1).message("批量删除失败").count(0l).build();
                }
            } catch (Exception e) {
                //遇到异常，返回异常原因
                e.printStackTrace();
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return ResponseTemplate.builder().code(1).message(e.getMessage()).count(0l).build();
            }
        }
        else {
            return ResponseTemplate.builder().code(1).message("传入参数为空").count(0l).build();
        }
    }
    /**
     *@Description:修改一条记录
     *@Param: [quotation]
     *@return: com.koocloud.electroplatemanage.common.ResponseTemplate
     *@Author: wjx
     *@date: 2020/11/16
     */
    @Override
    public ResponseTemplate update(Quotation quotation) {
        try {
            lock.lock();
            //没有异常并且修改数据条数大于0
            int rows = quotationMapper.update(quotation);
            if(rows>0) {
                return ResponseTemplate.builder().code(0).message("修改数据成功").count(1l).build();
            }
            else {//没有异常并且修改数据失败，原则上不出现
                return ResponseTemplate.builder().code(1).message("修改数据失败").count(0l).build();
            }
        } catch (Exception e) {
            //出现异常返回异常原因
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResponseTemplate.builder().code(1).message(e.getMessage()).count(0l).build();
        }finally {
            lock.unlock();
        }
    }
}
